package com.ruoyi.zyc.czjl.controller;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.zyc.czjl.domain.Czjl;
import com.ruoyi.zyc.czjl.service.ICzjlService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;

import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 成长记录Controller
 *
 * @author huacai
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/zyc/czjl")
public class CzjlController extends BaseController {
    @Resource
    private ICzjlService czjlService;

    /**
     * 查询成长记录列表
     */
    @PreAuthorize("@ss.hasPermi('zyc:czjl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Czjl czjl) {
        startPage();
        List<Czjl> list = czjlService.selectCzjlList(czjl);
        return getDataTable(list);
    }

    /**
     * 导出成长记录列表
     */
    @PreAuthorize("@ss.hasPermi('zyc:czjl:export')")
    @Log(title = "成长记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Czjl czjl) {
        List<Czjl> list = czjlService.selectCzjlList(czjl);
        ExcelUtil<Czjl> util = new ExcelUtil<Czjl>(Czjl.class);
        util.exportExcel(response, list, "成长记录数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Czjl> util = new ExcelUtil<Czjl>(Czjl.class);
        util.importTemplateExcel(response, "成长记录数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "成长记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('zyc:czjl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Czjl> util = new ExcelUtil<Czjl>(Czjl.class);
        InputStream inputStream = file.getInputStream();
        List<Czjl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = czjlService.batchInsertCzjl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取成长记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('zyc:czjl:query')")
    @GetMapping(value = "/{czjlId}")
    public AjaxResult getInfo(@PathVariable("czjlId") String czjlId) {
        return success(czjlService.selectCzjlByCzjlId(czjlId));
    }

    /**
     * 根据日期查询
     */
    @GetMapping(value = "/selectCzjlByRq/{rq}")
    public AjaxResult selectCzjlByRq(@PathVariable("rq") Date rq) {
        Czjl czjl = czjlService.selectCzjlByRq(rq);
        if (czjl == null) {
            return success("null");
        } else {
            return success(czjlService.selectCzjlByRq(rq));
        }
    }

    /**
     * 新增成长记录
     */
    @PreAuthorize("@ss.hasPermi('zyc:czjl:add')")
    @Log(title = "成长记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Czjl czjl) {
        return toAjax(czjlService.insertCzjl(czjl));
    }

    /**
     * 修改成长记录
     */
    @PreAuthorize("@ss.hasPermi('zyc:czjl:edit')")
    @Log(title = "成长记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Czjl czjl) {
        return toAjax(czjlService.updateCzjl(czjl));
    }

    /**
     * 删除成长记录
     */
    @PreAuthorize("@ss.hasPermi('zyc:czjl:remove')")
    @Log(title = "成长记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{czjlIds}")
    public AjaxResult remove(@PathVariable String[] czjlIds) {
        return toAjax(czjlService.deleteCzjlByCzjlIds(czjlIds));
    }
}
